/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.oalam.bookstore.dao;

import com.oalam.bookstore.domain.BusinessObjectFactory;
import java.sql.*;
import com.oalam.bookstore.domain.accounts.Account;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrateur
 */
public class SQLAccountDao implements AccountDao {

    protected Connection conn;

    public SQLAccountDao(boolean doUseTestdb) {
        try {
            //chargement du driver SQL pour le SGBD MySQL
            Class.forName("com.mysql.jdbc.Driver");

            // recup d'une connection au SGBD
            if(doUseTestdb)
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "admin");
            else
                conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore", "root", "admin");

        } catch (SQLException ex) {
            Logger.getLogger(SQLAccountDao.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(SQLAccountDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void finalize() throws Throwable {
        super.finalize();
        try {
            // fermeture des connections
            conn.close();
        } catch (SQLException ex) {
            Logger.getLogger(SQLAccountDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void parseAccount() {
        try {
            Statement stmt = conn.createStatement();
            String sql = "SELECT * FROM accounts";
            ResultSet rs = stmt.executeQuery(sql);
            BusinessObjectFactory boFactory = BusinessObjectFactory.getInstance();
            Account a = null;
            // iteration sur le result set
            while (rs.next()) {
                // retrieve record data
                String name = rs.getString("name");
                String email = rs.getString("email");
                String pwd = rs.getString("pwd");
                int id = rs.getInt("id");

                // setup account BO
                a = boFactory.createAccount(email);
                a.setName(name);
                a.setPwd(pwd);


                System.out.println(a.toString());
            }

            rs.close();
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SQLAccountDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Account a) {
        try {
            Statement stmt = conn.createStatement();
            String sql = "DELETE FROM accounts WHERE email='" + a.getEmail() + "'";
            boolean result = stmt.execute(sql);

            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SQLAccountDao.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    @Override
    public void deleteAll() {
        try {
            Statement stmt = conn.createStatement();
            String sql = "DELETE FROM accounts";
            boolean result = stmt.execute(sql);

            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SQLAccountDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public boolean find(Account a) {
        boolean found = false;
        try {
            Statement stmt = conn.createStatement();

            String sql = "SELECT * FROM accounts WHERE email='" + a.getEmail() + "'";
            ResultSet rs = stmt.executeQuery(sql);
            BusinessObjectFactory boFactory = BusinessObjectFactory.getInstance();

            // iteration sur le result set
            while (rs.next()) {
                found = true;
            }

            rs.close();
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SQLAccountDao.class.getName()).log(Level.SEVERE, null, ex);
        }

        return found;
    }

    @Override
    public void load(Account a) {
        try {
            Statement stmt = conn.createStatement();

            String sql = "SELECT * FROM accounts WHERE email='" + a.getEmail() + "'";
            ResultSet rs = stmt.executeQuery(sql);
            BusinessObjectFactory boFactory = BusinessObjectFactory.getInstance();

            // iteration sur le result set
            while (rs.next()) {
                // retrieve record data
                String name = rs.getString("name");
                String email = rs.getString("email");
                String pwd = rs.getString("pwd");
                int id = rs.getInt("id");

                // setup account BO
                a.setName(name);
                a.setPwd(pwd);


                System.out.println(a.toString());
            }

            rs.close();
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SQLAccountDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void save(Account a) {
        try {
            Statement stmt = conn.createStatement();

            String sql = "INSERT INTO accounts VALUES (null, '" + a.getName()
                    + "', '" + a.getPwd()
                    + "', '" + a.getEmail() + "')";

            stmt.executeUpdate(sql);
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(SQLAccountDao.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
